Skip of string arrays?

Hello,

I'm just struggling with a skip list which should have string arrays are content, i.e. something like this:

string strValues[5] = {"a", "b", "c", "d", "e"};
Skip skpList = create();

put(skpList, "array1", strValues);

string strResult[];

for strResult in skpList do
{
    print strResult[0] "\n"
}

When doing this in the DXL Editor, it's working fine, showing "a" as result.

However, having this in the production code (well, a bit more complex, as the skip list is filled and returned by another function), this leads to a DOORS crash. When checking the size of the "strResult" array, it shows something like "670412624", and the values are not valid.

string strTemp = "";
string strValues[];
int     intCount = 0;
                
for strValues in pSkpTypes do
{
        strTemp = (string key(pSkpTypes));
        
        debugPrint("Enumeration for: " strTemp " size: " sizeof(strValues) "");
                        
        for (intCount = 0; intCount < sizeof(strValues); intCount++)
        {
                debugPrint("--- value: " strValues[intCount] "");   
        }
}

I suspect the strResult[] declaration as problem, and the assignment by the "for" loop.

Do you have an idea what's wrong? As I require the string arrays (having different sizes), and the collection of them, can you think of an alternative?

Best regards,

Gerhard


GerhardS - Thu Feb 27 03:54:20 EST 2014

Re: Skip of string arrays?
David_G_Bond - Thu Feb 27 10:14:34 EST 2014

The first thing I notice is that the Skip list is defined with a string key, but you use create() to create it. If the key is a string you need to use createString()

 

- David Bond

Re: Skip of string arrays?
llandale - Fri Feb 28 13:29:21 EST 2014

Yes, use "createString" when the "key" is "string"; it is never a problem if you always do that.  But IIRC in this particular case since all the "key" values are inserted as string constants; it accidentally works in your example.

As for your problem .. well I cannot articulate it correctly, but you cannot use an "array" (lower case "a") as a container, hoping to change the address via the skip loop.  You can with an "Array" (upper case "A", which is a completely different construct).  In this case your skip loop assigns the 1st value of the "array" in the Skip to variable "strValues", which is declared at size zero.  The Skip loop does not assign the address of the embedded array to strValues.

In a related problem, if you declare an "array" inside a function, the space is reserved on the stack which is re-used when the function ends; which means if you attempt to "preserve" the "array", such as insert into a Skip, random results occur in the next function which resets that area of the stack that your "array" still points to.

The reality of DXL is that "array"s that are to be used by more than one sibling function, must be declared in the main context at a suitable size.  That is in practice often unrealistic since the size of the array is typically determined at run time (such as the number of Enumerations in a Type).  I once proved the concept of declaring some huge array in the main context and then "aliasing" the dynamic array to the next available location, keeping track of how much of the global was still available.  It was incomprehensible, but it worked.

Forget it, you can routinely replace the "array" with a "Skip", unless you figure to use the "array" in some kind of listView on some Dialog; in which case the space for it must persist; meaning it is in the main context.  Programming that is very awkward.

-Louie

Re: Skip of string arrays?
GerhardS - Sun Mar 02 07:11:26 EST 2014

llandale - Fri Feb 28 13:29:21 EST 2014

Yes, use "createString" when the "key" is "string"; it is never a problem if you always do that.  But IIRC in this particular case since all the "key" values are inserted as string constants; it accidentally works in your example.

As for your problem .. well I cannot articulate it correctly, but you cannot use an "array" (lower case "a") as a container, hoping to change the address via the skip loop.  You can with an "Array" (upper case "A", which is a completely different construct).  In this case your skip loop assigns the 1st value of the "array" in the Skip to variable "strValues", which is declared at size zero.  The Skip loop does not assign the address of the embedded array to strValues.

In a related problem, if you declare an "array" inside a function, the space is reserved on the stack which is re-used when the function ends; which means if you attempt to "preserve" the "array", such as insert into a Skip, random results occur in the next function which resets that area of the stack that your "array" still points to.

The reality of DXL is that "array"s that are to be used by more than one sibling function, must be declared in the main context at a suitable size.  That is in practice often unrealistic since the size of the array is typically determined at run time (such as the number of Enumerations in a Type).  I once proved the concept of declaring some huge array in the main context and then "aliasing" the dynamic array to the next available location, keeping track of how much of the global was still available.  It was incomprehensible, but it worked.

Forget it, you can routinely replace the "array" with a "Skip", unless you figure to use the "array" in some kind of listView on some Dialog; in which case the space for it must persist; meaning it is in the main context.  Programming that is very awkward.

-Louie

Thanks for your explanation, Louie. It seems to me that I have to use a different approach, perhaps using skip of DxlObjects. I'm working with attribute enumeration types, where you need the arrays, so it would have been pretty straight forward to have a skip list of arrays. 

- Hardy

Re: Skip of string arrays?
pommCannelle - Thu Mar 06 09:25:42 EST 2014

GerhardS - Sun Mar 02 07:11:26 EST 2014

Thanks for your explanation, Louie. It seems to me that I have to use a different approach, perhaps using skip of DxlObjects. I'm working with attribute enumeration types, where you need the arrays, so it would have been pretty straight forward to have a skip list of arrays. 

- Hardy

( or you can use the original container for your data ...
For example, a skip is not needed to store module types, the module have the info and access it is as simple as access to a skip list
-> for attributeType in myModule do  ...
And you don't need a skip to store values of a type
-> for i in 0:(attributeType.size-1) do print attributeType.strings[i] "\n"
Well ... nice skip feature, like find, is not available in this case ... but perhaps to developp it is not so hard ;) )
 

Re: Skip of string arrays?
Mathias Mamsch - Fri Mar 07 17:36:52 EST 2014

See here for a discussion on the probable problem and some more solutions:

https://www.ibm.com/developerworks/community/forums/html/topic?id=77777777-0000-0000-0000-000014267491&ps=25

By putting arrays to skips you probably have the same problem as when trying to returning an array, especially when you create the array inside a function. That is the probable reason why your example code works, but your production code is not. The example code has the array declared on global level.

Regards, Mathias